<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>

                    <div class="row">
                        <div class="col-sm-12">
                             <div class="box">
                                <div class="box-header with-border">
                                <h2 class="page-header">
                                    <i class="fa fa-globe"></i> <?php echo $site["name"];?> 产品分类
                                    <small class="pull-right"> <a href="<?php echo $site["domian"];?>" target="_blank"><?php echo $site["domian"];?></a></small>
                                </h2>

                                <?php echo $nav;?>

                                </div>
                                <div class="box-body">
                                
                                <div class="pull-right mb-10">
                                    <?php 
                                    if($checkapi):
                                    ?>
                                    <button class="btn btn-primary btn-sm btn-flat" id="sync"><i class="fa fa-shopping-cart"></i> 下载分类</button>
                                    <?php
                                    else:
                                    ?>
                                    <span class="label label-danger">API通讯错误</span>
                                    <?php
                                    endif;
                                    ?>
                                </div>

                                <?php if($data):?>
                                    <table class="table table-striped table-hover" style="margin-top:20px;">
                                        <thead>
                                            <tr>
                                               <!-- <th>ID</th> -->
                                                <th>名称</th>
                                                <th width="150">产品数量</th>
                                                <th width="150">状态</th>
                                                <th width="100">排序</th>
                                                <th width="50">管理</th>
                                            </tr>
                                        </thead>
                                        <tbody>
<?php foreach ($data as $item):?>
<tr shopid="<?php echo $item["id"]; ?>">
   <!-- <td><?php echo $item["id"]; ?></td> -->
    <td ondblclick="edit(this,'name')"><?php echo $item["name"]; ?></td>
    <td><?php echo $item["gcount"]; ?></td>
    <td><?php echo $item["rid"]>0?'<span class="label label-success">已同步</span>':'<span class="label label-danger">未同步</span>';?></td>
    <td ondblclick="edit(this,'sort')"><?php echo $item["sort"]; ?></td>
    <td>
    <button class="btn-xs btn-flat btn-danger btn-delete" itemid="<?php echo $item["id"];?>"><i class="fa fa-trash-o fa-lg"></i></button>
    </td>
</tr>
<?php if($item["sub"]):
            foreach($item["sub"] as $sitem):?>

<tr shopid="<?php echo $sitem["id"]; ?>">
   <!-- <td><?php echo $sitem["id"]; ?></td> -->
    <td ondblclick="edit(this,'name')" style="padding-left:50px;"><?php echo $sitem["name"]; ?></td>
    <td><?php echo $sitem["gcount"]; ?></td>
    <td><?php echo $sitem["rid"]>0?'<span class="label label-success">已同步</span>':'<span class="label label-danger">未同步</span>';?></td>
    <td ondblclick="edit(this,'sort')"><?php echo $sitem["sort"]; ?></td>
    <td>
    <button class="btn-xs btn-flat btn-danger btn-delete" itemid="<?php echo $sitem["id"];?>"><i class="fa fa-trash-o fa-lg"></i></button>
    </td>
</tr>

<?php       endforeach;
        endif;
?>

<?php endforeach;?>
                                        </tbody>
                                    </table>
                                    <?php endif;?>
                                    <?php 
                                    if(empty($data)){
                                        echo "<h2 style='text-align:center;padding:50px 0;'>没有查询到数据</h2>";
                                    }
                                    ?>
                                </div>
                            </div>
                         </div>
                    </div>
        


       
<script>
    function edit(obj,attrName){

        var oldHtml = obj.innerHTML;
        var newobj = document.createElement('input');
        newobj.name = attrName;
        newobj.type = "text";
        newobj.setAttribute("class","form-control input-sm");
        newobj.value = oldHtml;
        var cid = $(obj).parent().attr("shopid");     
        obj.innerHTML = '';  
        obj.appendChild(newobj);
        newobj.focus(); 

        $(newobj).keydown(function (event) {
            if (event.keyCode == 13) {
                $(this).trigger("blur");
            }
        });

        $(newobj).blur(function(){
            if(this.value==null || this.value==""){
                obj.innerHTML = oldHtml;
                return false;
            }
            obj.innerHTML = this.value?this.value:oldHtml;       
            if(oldHtml != this.value){
                $.ajax({
                    type:"post",
                    url:"/backend/sites/category/ajaxedit",
                    dataType:"json",
                    data:{"attrName":attrName,"newHtml":this.value,"cid":cid},
                    error:function(){
                    },
                    success:function(){
                    },
                });
            }  

        })
    }


  

    $("#sync").on("click",function(event){

        shadow = layer.load(1, {
            shade: [0.1,'#000'] 
        });
        url = window.location.href;
        url = url.replace(/index/,"sync");
        console.log(url);
        $.ajax({
                type:"post",
                url:url,
                dataType:"json",
                error:function(){
                    layer.msg("系统错误,请联系开发管理员");
                    layer.close(shadow);
                },
                success:function(result){
                    if(result["status"]==0)
                    {
                        layer.msg(result["msg"]);
                    }
                    else
                    {
                        layer.msg("同步成功");
                        window.location.href=window.location.href;
                    }
                    layer.close(shadow);
                },
        });
        event.preventDefault();
    });

    $("button.btn-delete").on("click",function(){
        var cid=$(this).attr("itemid");
        var row=$(this).parent().parent();
        layer.confirm("确认要删除吗，删除将无法回复？", { title: "确认" }, function (index) {
            $.ajax({
                type:"post",
                url:"/backend/sites/category/ajaxdel",
                dataType:"json",
                data:{"cid":cid},
                error:function(){
                },
                success:function(result){
                    if(result["status"]==1)
                    {
                        row.remove();
                    }
                    else
                    {
                        layer.msg(result["msg"]);
                    }
                },
            });
            layer.close(index); 
        });
    });



</script>
